<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ExportExcel
 *
 * @author Gehad
 */
class Lib_ExportExcel {

    public function __construct($name, $data) {
        $this->name = $name;
        $this->data = $data;
    }

    public function init() {
        $this->workBook = new Spreadsheet_Excel_Writer('reports/' . $this->name . '.xls');
//header background color
        define('PURPLE', 20);
        $this->workBook->setCustomColor(PURPLE, 128, 0, 128);
//add a style/format and return its reference
        $formatBold = & $this->workBook->addFormat();
        $formatBold->setBold();
        $formatHeader = &$this->workBook->addFormat(
                        array('Size' => 10,
                            'VAlign' => 'vcenter',
                            'HAlign' => 'center',
                            'Bold' => 1,
                            'Color' => 'white',
                            'FgColor' => PURPLE
                ));
//create a worksheet and return a reference
        $workSheet = & $this->workBook->addWorksheet('Products Report');
//write header
        $workSheet->write(0, 0, 'Id', $formatHeader);
        $workSheet->write(0, 1, 'Type', $formatHeader);
        $workSheet->write(0, 2, 'Title', $formatHeader);
        $workSheet->write(0, 3, 'Title type', $formatHeader);
        $workSheet->write(0, 4, 'Format', $formatHeader);
        $workSheet->write(0, 5, 'Created by', $formatHeader);
        $workSheet->write(0, 6, 'Supervisor', $formatHeader);
        $workSheet->write(0, 7, 'Language', $formatHeader);
        $workSheet->write(0, 8, 'Publisher', $formatHeader);
        $workSheet->write(0, 9, 'State', $formatHeader);
        $workSheet->write(0, 10, 'Authrization', $formatHeader);

//set column widths
        $workSheet->setColumn(1, 1, 16);
        $workSheet->setColumn(2, 2, 14);
        $workSheet->setColumn(1, 11, 20);
//your data is either from the database or from any other source
        foreach ($this->data as $key => $row) {
            //compute column position
            $x = $key + 1;
            //write now
            $workSheet->write($x, 0, $row['pid_val'], $formatBold);
            $workSheet->write($x, 1, $row['pid_type'], $formatBold);
            $workSheet->writeString($x, 2, $row['p_title'], $formatBold);
            $workSheet->writeString($x, 3, $row['tit_type'], $formatBold);
            $workSheet->writeString($x, 4, $row['frm_name'], $formatBold);
            $workSheet->writeString($x, 5, $row['crt_name'], $formatBold);
            $workSheet->writeString($x, 6, $row['sup_name'], $formatBold);
            $workSheet->writeString($x, 7, $row['lng_name'], $formatBold);
            $workSheet->writeString($x, 8, $row['pub_name'], $formatBold);
            $workSheet->writeString($x, 9, $row['state'], $formatBold);
            $workSheet->writeString($x, 10, $row['auth'], $formatBold);
        }
//closing the workbook will save the excel file to the server
        return $this->workBook->close();
    }

}

